<?php

/**
 * This is the model class for table "deal".
 *
 * The followings are the available columns in table 'deal':
 * @property string $id
 * @property string $deal_category_id
 * @property string $title
 * @property integer $discount
 * @property string $short_description
 * @property string $description
 * @property integer $is_approved
 * @property string $start_date
 * @property string $end_date
 * @property double $price
 * @property integer $on_main
 * @property integer $on_bonus
 *
 * The followings are the available model relations:
 * @property DealCategory $dealCategory
 * @property DealImg[] $dealImgs
 */
class Deal extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Deal the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'deal';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('deal_category_id, company_id, title, discount, price', 'required'),
			array('discount, is_approved, on_main, on_bonus', 'numerical', 'integerOnly'=>true),
			array('price', 'numerical'),
			array('deal_category_id', 'length', 'max'=>10),
			array('title', 'length', 'max'=>255),
			array('short_description, description, start_date, end_date', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, deal_category_id, title, discount, short_description, description, is_approved, start_date, end_date, price, on_main, on_bonus', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'company' => array(self::BELONGS_TO, 'Company', 'company_id'),
			'dealCategory' => array(self::BELONGS_TO, 'DealCategory', 'deal_category_id'),
			'dealImgs' => array(self::HAS_MANY, 'DealImg', 'deal_id'),
			'dealImg' => array(self::HAS_ONE, 'DealImg', 'deal_id', 'order'=>'id ASC'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'company_id' => 'Company',
			'deal_category_id' => 'Deal Category',
			'title' => 'Title',
			'discount' => 'Discount',
			'short_description' => 'short Description',
			'description' => 'Description',
			'is_approved' => 'Is Approved',
			'start_date' => 'Start Date',
			'end_date' => 'End Date',
			'price' => 'Price',
			'on_main' => 'On Main',
			'on_bonus' => 'On Bonus',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('company_id',$this->company_id,true);
		$criteria->compare('deal_category_id',$this->deal_category_id,true);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('discount',$this->discount);
		$criteria->compare('short_description',$this->short_description,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('is_approved',$this->is_approved);
		$criteria->compare('start_date',$this->start_date,true);
		$criteria->compare('end_date',$this->end_date,true);
		$criteria->compare('price',$this->price);
		$criteria->compare('on_main',$this->on_main);
		$criteria->compare('on_bonus',$this->on_bonus);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * conver dates for db format
	 */
	public function converDates() {
		if($this->start_date)
			$this->start_date = Functions::convertDateFormat($this->start_date);
			
		if($this->end_date)
			$this->end_date = Functions::convertDateFormat($this->end_date);
		
	}
	
	public function deleteImages() {
		$arr = Img::getImgsByDealId($this->id);
		if($arr)
			foreach($arr as $i => $img) {
				$img->deleteFile();
				$img->delete();
			}
	}
	
	public static function getModel($id) {
		return Deal::model()->findByAttributes(array('id'=>$id, 'is_approved'=>'1'));
	}
	
	/**
	 * Get list od deal for main page
	 */
	public static function getForMain() {
		return Deal::model()->findAllByAttributes(array('on_main'=>'1', 'is_approved'=>'1'));	
	}
	
	/**
	 * Get list od deal for bonus
	 */
	public static function getForBonus() {
		return Deal::model()->findAllByAttributes(array('on_bonus'=>'1', 'is_approved'=>'1'));
	}
	
	/**
	 * get first image
	 */
	public function getMainImg() {
		if($this->dealImg && ($obj = $this->dealImg->img)) {
			return $obj->attributes;			
		} else 
			return false;
	}
	
	public static function getByUserAndId($userId, $id) {
		$criteria = new CDbCriteria;
		$criteria->condition = 'id =:id AND user_id=:userId';
		$criteria->params = array(':userId'=>$userId, ':id'=>$id);
		
		return Deal::model()->find($criteria);
	} 
	
}